#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
using namespace std;
#define maxn 1005

int n,m;
vector<int> G[maxn];
int degree[maxn],sta[maxn][maxn],stnum[maxn];
bool park[maxn][maxn];
bool RecordedEdge[maxn][maxn];
struct Station {
	int station, level;
};
Station st(int station, int level){
	Station t; t.station=station; t.level=level; return t;
}
int maxans=0;

int read() {
	char c=getchar(); int x=0;
	while (c<'0' || c>'9') c=getchar();
	while (c>='0' && c<='9') {
		x=x*10+c-'0';
		c=getchar();
	}
	return x;
}
void topsort(){
	queue<Station> tque;
	for(int i=1;i<=n;++i){
		if(degree[i]==0){
			tque.push(st(i,1));
		}
	}
	while(!tque.empty()){
		Station c=tque.front(); tque.pop();
		int u=c.station, l=c.level;
		for(int i=0; i<G[u].size(); ++i){
			int v=G[u][i];
			--degree[v];
			if(degree[v]==0){
				tque.push(st(v,l+1));
				maxans = max(l+1, maxans);
			}
		}
		G[u].clear();
	}
}

int main(){
	n=read();
	m=read();
	for(int i=0;i<m;++i){
		stnum[i]=read();
		for(int j=0; j<stnum[i]; ++j){
			sta[i][j]=read();
			park[i][sta[i][j]]=true;
		}
	}
	for(int i=0;i<m;++i){
		for(int cst=sta[i][0]; cst<=sta[i][stnum[i]-1]; ++cst){
			if(park[i][cst]) continue;
			for(int tst=sta[i][0]; tst<=sta[i][stnum[i]-1]; ++tst){
				if(park[i][tst] && !RecordedEdge[cst][tst]){
					G[cst].push_back(tst);
					++degree[tst];
					RecordedEdge[cst][tst]=true;
				}
			}
		}
	}

	topsort();
	
	cout<<maxans<<endl;
	return 0;
}